const {Position} = require('./model');

//yinru
const PositionDao={
    //保存
    save(userInfo){
        return new Position(userInfo).save();
    },
    //获取数据
    // findByPage(page){
    //     let pageSize=5,
    //      _skip = (page -1)*pageSize,
    //      result={};

    //      Position.countDocuments().then(PositionDao.getData);
    // },
    //获取总页与当前页
    // getData(totalPage){
    //     var pages = Math.ceil(totalPage/pageSize);
    //     var currentPage = Math.min(page,pages);
    //     result.currentPage = currentPage;
    //     result.totalPage =totalPage;

    //     return Position.find().limit(pageSize).skip(_skip);
        
        
            
    // },
    //
    findByPage(page){
        let pageSize=5,
         _skip = (page -1)*pageSize,
         result={};

        return Position.countDocuments().then(function(totalPage){
            var pages = Math.ceil(totalPage/pageSize);
            console.log(page, pages)
            var currentPage = Math.min(page,pages);
            result.currentPage = currentPage;
            result.totalPage =totalPage;
    
           return Position.find().limit(pageSize).skip(_skip).then((doc)=>{
                result.doc=doc;
                return result;
            });
             
         });
         
    },

    
  
    
    //修改
    update(condition,result){
        return Position.update(condition,result)
    },
    //删除
    delete(condition){
        return Position.remove(condition);
    }

    
};
module.exports=PositionDao;